define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	require("./myscripts/SystemConfig");
	var fn = require("$UI/ls_common/fn");
	var Model = function() {
		this.callParent();
	};

	Model.prototype.modelParamsReceive = function(event) {
		this.comp("LoadingPopOver").hide();
		this.comp("LoadingPopOver").show();
		var self = this;
		setTimeout(function() {
			self.LoadLogData(self);
		}, 300);
	};

	Model.prototype.LoadLogData = function(self) {
		console.log("LoadLogData");
		self.comp("LoadingPopOver").hide();
		self.comp("LoadingPopOver").show();
		var api_url = window.app.base_url + window.app.FranchiserInfoApi;
		var data = {};
		data.username = window.app.username;
		data.password = window.app.password;
		data.CtmType = window.app.CtmType;

		console.log(data);
		self.comp("SendLogLists_Data").clear();
		console.time("请求时间");
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.timeEnd("请求时间");
				console.time("刷新时间");
				console.log(res);
				$.each(res, function(index, element) {
					// console.log(index);
					// console.log(element.CtmNameTwo);
					var List = self.comp("SendLogLists_Data").find([ "SN" ], [ element.SN ]);
					if (List.length > 0) {
						// console.log("重复的SN");
						self.comp("SendLogLists_Data").to(List[0].row);
						var objarr_str = "";
						var tmp_str = self.comp("SendLogLists_Data").getValue("Products");
						var tmp_objarr = fn.String_to_ObjectArray(tmp_str);
						var pro_obj = {};
						pro_obj.ID = element.ID;
						pro_obj.PrdNo = element.PrdNo;
						pro_obj.PrdName = element.PrdName;
						pro_obj.PrdCount = element.PrdCount;
						pro_obj.SN = element.SN;
						tmp_objarr.push(pro_obj);
						objarr_str = fn.ObjectArray_to_String(tmp_objarr);
						// console.log(objarr_str);
						self.comp("SendLogLists_Data").setValue("Products", objarr_str);
					} else {
						var pro_obj2 = {};
						pro_obj2.ID = element.ID;
						pro_obj2.PrdNo = element.PrdNo;
						pro_obj2.PrdName = element.PrdName;
						pro_obj2.PrdCount = element.PrdCount;
						pro_obj2.SN = element.SN;
						var objarr = [];
						objarr.push(pro_obj2);
						var objarr_str2 = fn.ObjectArray_to_String(objarr);
						// console.log(objarr_str2);
						self.comp("SendLogLists_Data").newData({
							"index" : -1,
							"defaultValues" : [ {
								"fID" : justep.UUID.createUUID(),
								"CustomerName" : element.CtmNameTwo,
								"Date" : element.DateDeliveryStr.substr(0, 8),
								"SN" : element.SN,
								"Products" : objarr_str2,
								"IsAudit" : element.IsAudit,
								"MainFID" : element.FID
							} ]
						});

					}
				});

				self.comp("list1").refresh();
				console.timeEnd("刷新时间");
				self.comp("LoadingPopOver").hide();
			},
			error : function(err) {
				console.log(err);
			}
		});
	};

	Model.prototype.LoadLogData_Sell = function(self) {
		console.log("LoadLogData");
		self.comp("LoadingPopOver").hide();
		self.comp("LoadingPopOver").show();
		var api_url = window.app.base_url + window.app.SellInfoApi;
		var data = {};
		data.username = window.app.username;
		data.password = window.app.password;
		console.log(data);
		self.comp("SendLogListsSell_Data").clear();
		console.time("请求时间");
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.timeEnd("请求时间");
				console.time("刷新时间");
				console.log(res);
				$.each(res, function(index, element) {
					var List = self.comp("SendLogListsSell_Data").find([ "SN" ], [ element.SN ]);
					if (List.length > 0) {
						// console.log("重复的SN");
						self.comp("SendLogListsSell_Data").to(List[0].row);
						var objarr_str = "";
						var tmp_str = self.comp("SendLogListsSell_Data").getValue("Products");
						var tmp_objarr = fn.String_to_ObjectArray(tmp_str);
						var pro_obj = {};
						pro_obj.ID = element.ID;
						pro_obj.PrdNo = element.PrdNo;
						pro_obj.PrdName = element.PrdName;
						pro_obj.PrdCount = element.PrdCount;
						pro_obj.SN = element.SN;
						tmp_objarr.push(pro_obj);
						objarr_str = fn.ObjectArray_to_String(tmp_objarr);
						// console.log(objarr_str);
						self.comp("SendLogListsSell_Data").setValue("Products", objarr_str);
					} else {
						var pro_obj2 = {};
						pro_obj2.ID = element.ID;
						pro_obj2.PrdNo = element.PrdNo;
						pro_obj2.PrdName = element.PrdName;
						pro_obj2.PrdCount = element.PrdCount;
						pro_obj2.SN = element.SN;
						var objarr = [];
						objarr.push(pro_obj2);
						var objarr_str2 = fn.ObjectArray_to_String(objarr);
						// console.log(objarr_str2);
						self.comp("SendLogListsSell_Data").newData({
							"index" : -1,
							"defaultValues" : [ {
								"fID" : justep.UUID.createUUID(),
								"SellName" : element.SellName,
								"DeliveryDate" : element.DeliveryDate.substr(0, 8),
								"SN" : element.SN,
								"Products" : objarr_str2,

							} ]
						});

					}
				});

				self.comp("list2").refresh();
				console.timeEnd("刷新时间");
				self.comp("LoadingPopOver").hide();
			},
			error : function(err) {
				console.log(err);
			}
		});
	};
	Model.prototype.list1AfterRender = function(event) {
		// console.log("listAfterRender");
		// console.log(event);

		var obj_arr = fn.String_to_ObjectArray(event.$object.val("Products"));
		$(event.elements[1]).find("[xid$='col13']").children().remove();
		obj_arr.forEach(function(e) {
			// debugger;
			// console.log(e);
			$("<label>产品:" + e.PrdName + " 数量:" + e.PrdCount + "</label><br/>").appendTo($(event.elements[1]).find("[xid$='col13']"));
		});

	};
	Model.prototype.list2AfterRender = function(event) {
//		console.log("list2AfterRender");
//		console.log(event);
		var obj_arr = fn.String_to_ObjectArray(event.$object.val("Products"));
		$(event.elements[1]).find("[xid$='col10']").children().remove();
		obj_arr.forEach(function(e) {
			// debugger;
//			console.log(e);
			$("<label>产品:" + e.PrdName + " 数量:" + e.PrdCount + "</label><br/>").appendTo($(event.elements[1]).find("[xid$='col10']"));
		});

	};

	Model.prototype.GoToAddProduct_ButtonClick = function(event) {
		console.log("GoToAddProduct_ButtonClick()");
		var click_row = event.bindingContext.$object;
		var params = {};
		params.FID = click_row.val('MainFID');
		params.products = click_row.val('Products');
		params.SN = click_row.val('SN');
		// TODO：IsAudit判断还没做
		this.getParent().comp("AddProducts_Window").load("AddProducts.w", params);
		this.getParent().comp("Main_Contents").to("AddProducts_Content");
	};

	Model.prototype.DeleteItemClick = function(event) {
		console.log("DeleteItemClick()");
		var click_row = event.bindingContext.$object;
		var data = {};
		data.username = window.app.username;
		data.password = window.app.password;
		data.SN = click_row.val('SN');
		console.log(data);
		var api_url = window.app.base_url + window.app.FranchiserDeleteApi;
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.log(res);

			},
			error : function(err) {
				console.log(err);
			}
		});
		this.LoadLogData(this);
	};

	Model.prototype.LogBtnClick = function(event) {
		this.LoadLogData(this);
		this.comp("contents1").to("content1");
		return;
	};
	Model.prototype.SellBtnClick = function(event) {
		this.LoadLogData_Sell(this);
		this.comp("contents1").to("content4");
		return;
	};

	return Model;
});